{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.keys import Keys\n",
    "from selenium.webdriver.chrome.options import Options\n",
    "import selenium.webdriver.support.expected_conditions as EC\n",
    "from selenium.webdriver.support.wait import WebDriverWait\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "北京法院审判信息网-开庭公告\n"
     ]
    }
   ],
   "source": [
    "chrome_options = Options()\n",
    "# chrome_options.add_argument('--headless')      # 设置无头浏览器\n",
    "chrome_options.add_argument('--disable-gpu')\n",
    "chrome_options.binary_location = r'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe'\n",
    "driver = webdriver.Chrome(chrome_options=chrome_options)\n",
    "driver.get('http://www.bjcourt.gov.cn/ktgg/index.htm')\n",
    "print (driver.title)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<div class=\"search_menu\" id=\"search_menu\">\n",
      "\t\t<div class=\"search_value_box\">\n",
      "\t\t\t<span class=\"sp_gain_value\">裁判文书</span>\n",
      "\t\t\t<span class=\"sp_mousedown\" style=\"float: left;\" id=\"sp_mousedown\"></span>\n",
      "\t\t    <input type=\"hidden\" id=\"searchType1\" value=\"cpws\">\n",
      "\t\t    <input type=\"hidden\" id=\"searchCondtion\" value=\"\">\n",
      "\t\t</div>\n",
      "\t\t<!-- search_value_box end -->\n",
      "\t\t<dl class=\"search_con_nav\" id=\"search_con_nav1\">\n",
      "\t\t\t<dd value=\"cpws\">裁判文书</dd>\n",
      "\t\t\t<dd value=\"ktgg\">开庭公告</dd>\n",
      "\t\t\t<dd value=\"bggs\">减刑假释类公示</dd>\n",
      "\t\t\t<dd value=\"other\">其他</dd>\n",
      "\t\t\t<dd value=\"totalIndex\">全站检索</dd>\n",
      "\t\t</dl>\n",
      "\t\t<!-- search_con_nav end -->\n",
      "\t</div>\n"
     ]
    }
   ],
   "source": [
    "table_div = driver.find_element_by_xpath(\".//div[@class='main_aside fr']\")\n",
    "caipan = table_div.find_element_by_id('search_menu')\n",
    "print (caipan.get_attribute('outerHTML'))\n",
    "# print ('内容是：',caipan.get_attribute('innerHTML'))\n",
    "# print ('文本是：',caipan.text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<div class=\"search_menu\" id=\"search_menu\">\n",
      "\t\t<div class=\"search_value_box\">\n",
      "\t\t\t<span class=\"sp_gain_value\">裁判文书</span>\n",
      "\t\t\t<span class=\"sp_mousedown\" style=\"float: left;\" id=\"sp_mousedown\"></span>\n",
      "\t\t    <input type=\"hidden\" id=\"searchType1\" value=\"cpws\">\n",
      "\t\t    <input type=\"hidden\" id=\"searchCondtion\" value=\"\">\n",
      "\t\t</div>\n",
      "\t\t<!-- search_value_box end -->\n",
      "\t\t<dl class=\"search_con_nav\" id=\"search_con_nav1\">\n",
      "\t\t\t<dd value=\"cpws\">裁判文书</dd>\n",
      "\t\t\t<dd value=\"ktgg\">开庭公告</dd>\n",
      "\t\t\t<dd value=\"bggs\">减刑假释类公示</dd>\n",
      "\t\t\t<dd value=\"other\">其他</dd>\n",
      "\t\t\t<dd value=\"totalIndex\">全站检索</dd>\n",
      "\t\t</dl>\n",
      "\t\t<!-- search_con_nav end -->\n",
      "\t</div>\n"
     ]
    }
   ],
   "source": [
    "caipan_2 = table_div.find_element_by_xpath(\".//*[@id='search_menu']\")\n",
    "print (caipan_2.get_attribute('outerHTML'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<a href=\"/fygg/index.htm?c=100002005\" title=\"拍卖公告\" class=\"fygg_03\">拍卖公告</a>\n"
     ]
    }
   ],
   "source": [
    "gonggao = driver.find_element_by_xpath(\".//div[@class='left_nav']\")\n",
    "paimai = gonggao.find_element_by_link_text(\"拍卖公告\")\n",
    "print (paimai.get_attribute('outerHTML'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<a href=\"/fygg/index.htm?c=100002005\" title=\"拍卖公告\" class=\"fygg_03\">拍卖公告</a>\n"
     ]
    }
   ],
   "source": [
    "paimai_2 = gonggao.find_element_by_partial_link_text(\"拍卖\")\n",
    "print (paimai_2.get_attribute('outerHTML'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'height': 38, 'width': 228}\n",
      "{'y': 362, 'x': 8}\n"
     ]
    }
   ],
   "source": [
    "print (paimai.size)\n",
    "print (paimai.location)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "print (driver.page_source)         "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "driver.set_window_size(1280,600)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "driver.maximize_window()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "driver.get_screenshot_as_file(r\"C:\\screen\\11.png\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'domain': '.www.bjcourt.gov.cn',\n",
       "  'httpOnly': False,\n",
       "  'name': '__ads_session',\n",
       "  'path': '/',\n",
       "  'secure': False,\n",
       "  'value': 'FPcM2GWPCgnW2f5FWgA='},\n",
       " {'domain': 'www.bjcourt.gov.cn',\n",
       "  'httpOnly': False,\n",
       "  'name': 'COLLCK',\n",
       "  'path': '/ktgg',\n",
       "  'secure': False,\n",
       "  'value': '3690713922'},\n",
       " {'domain': 'www.bjcourt.gov.cn',\n",
       "  'httpOnly': False,\n",
       "  'name': 'vh2PJqrvyx',\n",
       "  'path': '/',\n",
       "  'secure': False,\n",
       "  'value': 'MDAwM2IyYWYxZTQwMDAwMDAwMDQwEiFXNzoxNTE2ODkzMjMx'},\n",
       " {'domain': 'www.bjcourt.gov.cn',\n",
       "  'httpOnly': False,\n",
       "  'name': '6JDgKK8lEy',\n",
       "  'path': '/',\n",
       "  'secure': False,\n",
       "  'value': 'MDAwM2IyYWYxZTQwMDAwMDAwMDMwIVtcIEoxNTE2ODkzMjMx'},\n",
       " {'domain': 'www.bjcourt.gov.cn',\n",
       "  'httpOnly': False,\n",
       "  'name': 'JSESSIONID',\n",
       "  'path': '/',\n",
       "  'secure': False,\n",
       "  'value': 'F2AABBAF6C586563AA13FA4A3733CD09'},\n",
       " {'domain': 'www.bjcourt.gov.cn',\n",
       "  'httpOnly': False,\n",
       "  'name': 'COLLCK',\n",
       "  'path': '/',\n",
       "  'secure': False,\n",
       "  'value': '3690713922'}]"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "driver.get_cookies()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'domain': 'www.bjcourt.gov.cn',\n",
       " 'httpOnly': False,\n",
       " 'name': 'COLLCK',\n",
       " 'path': '/ktgg',\n",
       " 'secure': False,\n",
       " 'value': '3690713922'}"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "driver.get_cookie('COLLCK')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'domain': 'www.bjcourt.gov.cn',\n",
       " 'expiry': 2147910522,\n",
       " 'httpOnly': False,\n",
       " 'name': 'beijing',\n",
       " 'path': '/',\n",
       " 'secure': False,\n",
       " 'value': 'court'}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cook = {'name':'beijing', 'value':'court'}\n",
    "driver.add_cookie(cook)\n",
    "driver.get_cookie('beijing')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "driver.delete_cookie('beijing')\n",
    "driver.get_cookie('beijing')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CDwindow-(C756D72E0E0E6B77D495BC41F8399D9B)\n"
     ]
    }
   ],
   "source": [
    "start_page_handle = driver.current_window_handle\n",
    "print (start_page_handle)\n",
    "key_div = table_div.find_element_by_xpath(\".//div[@class='search_all_wrap']\")\n",
    "key_input = key_div.find_element_by_tag_name('input')           # 使用标签名来定位\n",
    "key_input.clear()\n",
    "key_input.send_keys(\"中国进出口银行\")\n",
    "submit_button = key_div.find_element_by_class_name('search_all_btn')\n",
    "submit_button.click() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CDwindow-(C756D72E0E0E6B77D495BC41F8399D9B)\n"
     ]
    }
   ],
   "source": [
    "print (driver.current_window_handle)    # 句柄没有切换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "法院名称 全部\n",
      "法院名称 北京市高级人民法院\n",
      "法院名称 北京市第一中级人民法院\n",
      "法院名称 北京市第二中级人民法院\n",
      "法院名称 北京市第三中级人民法院\n",
      "法院名称 北京市第四中级人民法院\n",
      "法院名称 北京知识产权法院\n",
      "法院名称 北京市东城区人民法院\n",
      "法院名称 北京市西城区人民法院\n",
      "法院名称 北京市朝阳区人民法院\n",
      "法院名称 北京市海淀区人民法院\n",
      "法院名称 北京市丰台区人民法院\n",
      "法院名称 北京市石景山区人民法院\n",
      "法院名称 北京市门头沟区人民法院\n",
      "法院名称 北京市房山区人民法院\n",
      "法院名称 北京市通州区人民法院\n",
      "法院名称 北京市大兴区人民法院\n",
      "法院名称 北京市顺义区人民法院\n",
      "法院名称 北京市昌平区人民法院\n",
      "法院名称 北京市怀柔区人民法院\n",
      "法院名称 北京市平谷区人民法院\n",
      "法院名称 北京市密云区人民法院\n",
      "法院名称 北京市延庆区人民法院\n",
      "法院名称 北京铁路运输法院\n"
     ]
    }
   ],
   "source": [
    "fy_div = table_div.find_element_by_xpath(\".//div[@class='d_column_01']//ul[@class='search_court_select_box']\")\n",
    "fy_list = fy_div.find_elements_by_tag_name('a')\n",
    "for fy in fy_list:\n",
    "    print ('法院名称', fy.get_attribute('innerHTML'))\n",
    "fy_buttom = table_div.find_element_by_id('sp_mousedown_court')\n",
    "fy_buttom.click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "法院名称 北京市高级人民法院\n"
     ]
    }
   ],
   "source": [
    "bjgj_fy = fy_div.find_elements_by_tag_name('a')[1]\n",
    "print ('法院名称', bjgj_fy.get_attribute('innerHTML'))\n",
    "bjgj_fy.click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "submit_button = table_div.find_element_by_id('searchbtn')\n",
    "submit_button.click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "driver.back()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "链接为 http://www.bjcourt.gov.cn/ktgg/ktggDetailInfo.htm?NId=95590&NAjbh=110013785 标题为 2月28日（星期三） 14:00，北京市东城区人民法院定于二〇一八年二月二十八日 下午十四时整在（北区）第20法庭公开开庭审理山东名君纺织服装...\n",
      "链接为 http://www.bjcourt.gov.cn/ktgg/ktggDetailInfo.htm?NId=12224&NAjbh=42563176 标题为 2月28日（星期三） 9:00，北京市石景山区人民法院定于二〇一八年二月二十八日 上午九时整在二办公区第二法庭公开开庭审理趣游时代（北京）科技...\n",
      "链接为 http://www.bjcourt.gov.cn/ktgg/ktggDetailInfo.htm?NId=12225&NAjbh=42563175 标题为 2月28日（星期三） 9:00，北京市石景山区人民法院定于二〇一八年二月二十八日 上午九时整在二办公区第二法庭公开开庭审理趣游时代（北京）科技...\n"
     ]
    }
   ],
   "source": [
    "item_div = table_div.find_element_by_xpath(\".//ul[@class='ul_news_long']\")\n",
    "item_list = item_div.find_elements_by_tag_name('a')\n",
    "for item in item_list[:3]:\n",
    "    link = item.get_attribute('href')\n",
    "    title = item.text.strip()\n",
    "    print ('链接为', link, '标题为', title)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "内容是 本院定于二〇一八年二月二十八日 下午十四时整在（北区）第20法庭公开开庭审理山东名君纺织服装有限公司、中少新雏鹰（北街）服饰有限公司 特许经营合同纠纷一案。\n",
      "内容是 本院定于二〇一八年二月二十八日 上午九时整在二办公区第二法庭公开开庭审理趣游时代（北京）科技有限公司、深圳市腾讯计算机系统有限公司、腾讯科技（深圳）有限公司 侵害作品信息网络传播权纠纷一案。\n",
      "内容是 本院定于二〇一八年二月二十八日 上午九时整在二办公区第二法庭公开开庭审理趣游时代（北京）科技有限公司、深圳市腾讯计算机系统有限公司、腾讯科技（深圳）有限公司 侵害作品信息网络传播权纠纷一案。\n"
     ]
    }
   ],
   "source": [
    "for item in item_list[:3]:\n",
    "    item.click()\n",
    "    all_handles = driver.window_handles\n",
    "    for handle in driver.window_handles:\n",
    "        if handle != start_page_handle:\n",
    "            driver.switch_to.window(handle)\n",
    "            content = driver.find_element_by_xpath(\".//div[@class='article']\")\n",
    "            neirong = content.find_element_by_xpath(\".//p[@class='p_text p_ktgg_content']\")\n",
    "            print ('内容是', neirong.text.strip())\n",
    "    driver.close()\n",
    "    driver.switch_to.window(start_page_handle)\n",
    "    time.sleep(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "翻转第1页\n",
      "翻转第2页\n",
      "翻转第3页\n"
     ]
    }
   ],
   "source": [
    "for p in range(3):\n",
    "    page_new = driver.find_element_by_id('smart-paginator').find_elements_by_tag_name('a')[p]\n",
    "    print ('翻转第%d页'%(p+1))\n",
    "    page_new.click()\n",
    "    driver.get_screenshot_as_file(r\"C:\\screen\\%d.png\"%(p+1))\n",
    "    time.sleep(3)\n",
    "    if p == 2:\n",
    "        driver.back()\n",
    "        driver.get_screenshot_as_file(r\"C:\\screen\\a2.png\")\n",
    "        time.sleep(2)\n",
    "        driver.forward()\n",
    "        driver.get_screenshot_as_file(r\"C:\\screen\\a3.png\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "网页源码 <html xmlns=\"http://www.w3.org/1999/xhtml\"><head>\n",
      "<meta http-equiv=\"content-type\" content=\"text/html; charset=gb2312\" />\n",
      "<title> 您的IP地址 </title>\n",
      "</head>\n",
      "<body style=\"margin:0px\"><center>您的IP是：[110.72.35.136] 来自：广西壮族自治区贵港市 联通</center></body></html>\n"
     ]
    },
    {
     "ename": "NoSuchWindowException",
     "evalue": "Message: no such window: target window already closed\nfrom unknown error: web view not found\n  (Session info: chrome=63.0.3239.132)\n  (Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 6.3.9600 x86_64)\n",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNoSuchWindowException\u001b[0m                     Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-56-ee887c9518f9>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m     11\u001b[0m     \u001b[0mdriver\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'http://2017.ip138.com/ic.asp'\u001b[0m\u001b[1;33m)\u001b[0m  \u001b[1;31m# 检查代理是否成功 http://2017.ip138.com/ic.asp  http://www.ip138.com/\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m     12\u001b[0m     \u001b[0mdriver\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mimplicitly_wait\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m---> 13\u001b[0;31m     \u001b[1;32mif\u001b[0m \u001b[1;34m'IP地址'\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mdriver\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpage_source\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     14\u001b[0m         \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;34m'网页源码'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdriver\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpage_source\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m     15\u001b[0m     \u001b[0mdriver\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mclose\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mE:\\ProgramFiles\\Anaconda3\\lib\\site-packages\\selenium\\webdriver\\remote\\webdriver.py\u001b[0m in \u001b[0;36mpage_source\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m    582\u001b[0m             \u001b[0mdriver\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpage_source\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    583\u001b[0m         \"\"\"\n\u001b[0;32m--> 584\u001b[0;31m         \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexecute\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mCommand\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mGET_PAGE_SOURCE\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'value'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    585\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    586\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mclose\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mE:\\ProgramFiles\\Anaconda3\\lib\\site-packages\\selenium\\webdriver\\remote\\webdriver.py\u001b[0m in \u001b[0;36mexecute\u001b[0;34m(self, driver_command, params)\u001b[0m\n\u001b[1;32m    306\u001b[0m         \u001b[0mresponse\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcommand_executor\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexecute\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdriver_command\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mparams\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    307\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mresponse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 308\u001b[0;31m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0merror_handler\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcheck_response\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresponse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    309\u001b[0m             response['value'] = self._unwrap_value(\n\u001b[1;32m    310\u001b[0m                 response.get('value', None))\n",
      "\u001b[0;32mE:\\ProgramFiles\\Anaconda3\\lib\\site-packages\\selenium\\webdriver\\remote\\errorhandler.py\u001b[0m in \u001b[0;36mcheck_response\u001b[0;34m(self, response)\u001b[0m\n\u001b[1;32m    192\u001b[0m         \u001b[1;32melif\u001b[0m \u001b[0mexception_class\u001b[0m \u001b[1;33m==\u001b[0m \u001b[0mUnexpectedAlertPresentException\u001b[0m \u001b[1;32mand\u001b[0m \u001b[1;34m'alert'\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mvalue\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    193\u001b[0m             \u001b[1;32mraise\u001b[0m \u001b[0mexception_class\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmessage\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mscreen\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstacktrace\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mvalue\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'alert'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'text'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 194\u001b[0;31m         \u001b[1;32mraise\u001b[0m \u001b[0mexception_class\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmessage\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mscreen\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstacktrace\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    195\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    196\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m_value_or_default\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mobj\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdefault\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mNoSuchWindowException\u001b[0m: Message: no such window: target window already closed\nfrom unknown error: web view not found\n  (Session info: chrome=63.0.3239.132)\n  (Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 6.3.9600 x86_64)\n"
     ]
    }
   ],
   "source": [
    "proxy_list = []\n",
    "f = open(r'C:\\Users\\huaixuan.guan\\Desktop\\\\proxy.txt', \"r\")\n",
    "lines = f.readlines()\n",
    "for line in lines:\n",
    "    if line:\n",
    "        line = line.strip()\n",
    "        proxy_list.append(line)\n",
    "for pr in proxy_list:\n",
    "    chrome_options.add_argument('--proxy-server=%s' % pr)\n",
    "    driver = webdriver.Chrome(chrome_options=chrome_options)\n",
    "    driver.get('http://2017.ip138.com/ic.asp')  # 检查代理是否成功 http://2017.ip138.com/ic.asp  http://www.ip138.com/\n",
    "    driver.implicitly_wait(10)\n",
    "    if 'IP地址' in driver.page_source:\n",
    "        print ('网页源码', driver.page_source)\n",
    "    driver.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
